﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;

using System.Windows.Media;
using System.Windows.Media.Animation;


namespace RunCoachMobileApp.View
{
    public partial class MainPage : PhoneApplicationPage
    {
        /*App app = App.Current as App;
        Popup p;
        PasswordBox pb, pb2;
        TextBlock tb = null;*/

        public MainPage()
        {
            InitializeComponent();
        }

        /*private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!app.IsAuthenticated)
            {
                p = new Popup();
                LayoutRoot.Children.Add(p);
                ApplicationBar.IsVisible = false;

                // Set where the popup will show up on the screen.
                p.VerticalOffset = 60;
                p.HorizontalOffset = 25;

                Border border = new Border();
                border.BorderBrush = new SolidColorBrush(Colors.White);
                border.BorderThickness = new Thickness(5.0);

                StackPanel panel1 = new StackPanel();
                panel1.Background = new SolidColorBrush(Colors.Black);
                panel1.Width = LayoutRoot.ActualWidth - 2 * p.HorizontalOffset;

                Button button1 = new Button();
                button1.Content = "OK";
                button1.Margin = new Thickness(5.0);
                button1.Click += new RoutedEventHandler(loginbutton_Click);
                TextBlock textblock1 = new TextBlock();
                textblock1.TextWrapping = TextWrapping.Wrap;
                if (app.FirstRun)
                    textblock1.Text = "Please enter your choice of a password:";
                else
                    textblock1.Text = "Please enter your password";
                textblock1.Margin = new Thickness(5.0);
                textblock1.FontSize = 30;
                textblock1.Foreground = new SolidColorBrush(Colors.White);
                pb = new PasswordBox();
                pb.KeyDown += new KeyEventHandler(pb_KeyDown);
                panel1.Children.Add(textblock1);
                panel1.Children.Add(pb);
                if (app.FirstRun)
                {
                    TextBlock textblock2 = new TextBlock();
                    textblock2.TextWrapping = textblock1.TextWrapping;
                    textblock2.Margin = textblock1.Margin;
                    textblock2.FontSize = textblock1.FontSize;
                    textblock2.Foreground = textblock1.Foreground;
                    textblock2.Text = "Please repeat the password";
                    pb2 = new PasswordBox();
                    panel1.Children.Add(textblock2);
                    panel1.Children.Add(pb2);
                }
                panel1.Children.Add(button1);
                border.Child = panel1;

                // Set the Child property of Popup to the border 
                // which contains a stackpanel, textblock and button.
                p.Child = border;

                // Open the popup.
                p.IsOpen = true;
                pb.Focus();
            }
            else
                // Set the data context of the listbox control to the sample data
                DataContext = App.ViewModel;
        }

        void pb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                loginbutton_Click(this, new RoutedEventArgs());
        }

        private void loginbutton_Click(object sender, RoutedEventArgs e)
        {
            // Close the popup.
            p.IsOpen = false;
            if (app.FirstRun)
            {
                if (!pb.Password.Equals(pb2.Password))
                {
                    MessageBox.Show("The passwords do not match. Please try again.");
                    p.IsOpen = true;
                    pb.Password = "";
                    pb2.Password = "";
                    pb.Focus();
                    return;
                }
                app.PwdHash = CryptoUtil.GetHashCode(pb.Password);
                app.IsAuthenticated = true;
                app.FirstRun = false;
                App.ViewModel.LoadData();
            }
            else
                app.IsAuthenticated = CryptoUtil.GetHashCode(pb.Password).CompareTo(app.PwdHash) == 0;

            if (app.IsAuthenticated)
            {
                DataContext = App.ViewModel;
                ApplicationBar.IsVisible = true;
            }
            else
            {
                tb = new TextBlock();
                tb.Text = "Worng password.";
                tb.FontSize = 36;
                tb.Foreground = new SolidColorBrush(Colors.Red);
                tb.Margin = new Thickness(12);
                tb.SetValue(Grid.RowProperty, 2);
                LayoutRoot.Children.Add(tb);
                ApplicationBar.IsVisible = true;
                (ApplicationBar.MenuItems[0] as ApplicationBarMenuItem).IsEnabled = false;

                DataContext = null;
            }
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
         {
         if (p.IsOpen)
            {
            loginbutton_Click(this, new RoutedEventArgs());
            e.Cancel = true;
            }
         }*/
      
    }
}